package com.smcdd.cdb.dbo.cdb_dbo.service.maneger;

import android.content.Context;


import com.smcdd.cdb.dbo.cdb_dbo.network.RetrofitHelper;
import com.smcdd.cdb.dbo.cdb_dbo.network.RetrofitService;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.BaseResponseEntity;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Charging;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Functions;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Login;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.LoginCode;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.ParseCode;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Simple;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.StoreInfo;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.StoreList;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Tel;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.UpdateVersion;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.UploadImage;
import com.smcdd.cdb.dbo.cdb_dbo.service.exception.ExceptionEngine;
import com.smcdd.cdb.dbo.cdb_dbo.service.exception.ServerException;

import java.util.List;
import java.util.Map;

import okhttp3.RequestBody;
import okhttp3.Response;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 作者：Administrator on 2017/6/8 18:01
 * 邮箱：zhanghuaiha@gmail.com
 */

public class DataManager {
    private RetrofitService mRetrofitService;
    public DataManager(Context context){
        mRetrofitService = RetrofitHelper.getInstance(context).getServer();
    }
    //错误回调
    private class ServerResultFunc<T> implements Func1<BaseResponseEntity<T>, T> {
        @Override
        public T call(BaseResponseEntity<T> httpResult) {
            if (httpResult.code != 200) {
                throw new ServerException(httpResult.code,httpResult.message);
            }
            return httpResult.data;
        }
    }



    private class HttpResultFunc<T> implements Func1<Throwable, Observable<T>> {
        @Override
        public Observable<T> call(Throwable throwable) {
            return Observable.error(ExceptionEngine.handleException(throwable));
        }
    }



    //获取新版本
    public Observable<UpdateVersion> getVersion(String rf, String ts, String v, String i, String udid, String encry){
        return mRetrofitService.getVersion(rf,ts,v,i,udid,encry);
    }

//    //获取验证码
//    public Observable<LoginCode> getCode(String rf, String ts, String v, String i, String udid, String mobile, String encry){
//        return mRetrofitService.getCode(rf,ts,v,i,udid,mobile,encry);
//    }
    //登录
    public Observable<BaseResponseEntity<Login>> Login(String rf, String ts, String v, String i, String udid, String mobile, String code, String device_type, String brand, String model, String version_sdk, String version_release, String software_version, String encry){
        return mRetrofitService.login(rf,ts,v,i,udid,mobile,code,device_type,brand,model,version_sdk,version_release,software_version,encry);
    }

    //判断当前电话号码是否可用
    public Observable<Tel> getTel(String rf, String ts, String v, String i, String udid, String mobile, String encry){
        return mRetrofitService.getTel(rf,ts,v,i,udid,mobile,encry);
    }

    //判断当前电话号码是否可用
    public Observable<BaseResponseEntity> getDemo(String rf, String ts, String v, String i, String udid, String mobile, String encry){
        return mRetrofitService.getDemo(rf,ts,v,i,udid,mobile,encry)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    //获取9宫格数据
    public Observable<Functions> getFunctions(String rf, String ts, String v, String i, String udid, String eid, String encry){
        return mRetrofitService.getFunctions(rf,ts,v,i,udid,eid,encry);
    }

    //扫一扫数据解析
    public Observable<ParseCode> getParse(String rf, String ts, String v, String i, String udid, String eid, String qrcode, String encry){
        return mRetrofitService.getParse(rf,ts,v,i,udid,eid,qrcode,encry);
    }

    //扫一扫数据解析
    public Observable<BaseResponseEntity<String>> getStart2charging(String rf, String ts, String v, String i, String udid, String alias,String eid, String encry){
        return mRetrofitService.getStart2charging(rf,ts,v,i,udid,alias,eid,encry);
    }

    //获取商家信息
    public Observable<StoreInfo> getStoreInfo(String rf, String ts, String v, String i, String udid, String eid, String page_size,String page_index,String status, String encry){
        return mRetrofitService.getStoreInfo(rf,ts,v,i,udid,eid,page_size,page_index,status,encry);
    }

    //所属设备
    public Observable<Charging> getCharging2Page(String rf, String ts, String v, String i, String udid, String id,String eid, String page_size, String page_index,  String encry){
        return mRetrofitService.getCharging2Page(rf,ts,v,i,udid,id,eid,page_size,page_index,encry);
    }

    //设备撤回
    public Observable<BaseResponseEntity<String>> getWithdraw(String rf, String ts, String v, String i, String udid,String alias, String eid,String encry){
        return mRetrofitService.getWithdraw(rf,ts,v,i,udid,alias,eid,encry);
    }

    //硬件故障
    public Observable<BaseResponseEntity> getHardware(String rf, String ts, String v, String i, String udid,String alias, String eid,String content,String encry){
        return mRetrofitService.getHardware(rf,ts,v,i,udid,alias,eid,content,encry);
    }

    //删除
    public Observable<BaseResponseEntity> Delete(String rf, String ts, String v, String i, String udid, String id,String eid,String encry){
        return mRetrofitService.getDelete(rf,ts,v,i,udid,id,eid,encry);
    }

    //将店铺设为已铺设
    public Observable<BaseResponseEntity> getLaid(String rf, String ts, String v, String i, String udid, String id,String eid,String encry){
        return mRetrofitService.getLaid(rf,ts,v,i,udid,id,eid,encry);
    }

    //激活设备
    public Observable<BaseResponseEntity> getActivation(String rf, String ts, String v, String i, String udid,String eid, String alias,String name,String remark, String store_info_id,  String encry){
        return mRetrofitService.getActivation(rf,ts,v,i,udid,eid,alias,name,remark,store_info_id,encry);
    }

    //所属店铺
    public Observable<StoreInfo> getStoreInfos(String rf, String ts, String v, String i, String udid, String eid, String page_size,String page_index,String encry){
        return mRetrofitService.getStoreInfos(rf,ts,v,i,udid,eid,page_size,page_index,encry);
    }

    //新增店铺
    public Observable<BaseResponseEntity> insert(String rf, String ts, String v, String i, String udid, String eid, String name,String contact,String tel,String latitude,String longitude,String province_id,String city_id,String area_id,String specific_address,String province_name,String city_name,String area_name,String position,String position_name,String img_url,String from_type,String remark,String encry){
        return mRetrofitService.insert(rf,ts,v,i,udid,eid,name,contact,tel,latitude,longitude,province_id,city_id,area_id,specific_address,province_name,city_name,area_name,position,position_name,img_url,from_type,remark,encry);
    }

    //上传图片 废弃
    public Observable<UploadImage> uploadImage(String encry, String i, String rf, String ts, String udid, String v,Map<String, RequestBody> map){
        return mRetrofitService.uploadImage(encry,i,rf,ts,udid,v,map);
    }
    //上传图片
    public Observable<UploadImage> uploadImages(Map<String, RequestBody> map){
        return mRetrofitService.uploadImages(map);
    }

    //更新门店
    public Observable<BaseResponseEntity> update(String rf, String ts, String v, String i,String udid,String id, String eid, String name,String contact,String tel,String latitude,String longitude,String province_id,String city_id,String area_id,String specific_address,String province_name,String city_name,String area_name,String position,String position_name,String img_url,String from_type,String remark,String encry){
        return mRetrofitService.update(rf,ts,v,i,udid,id,eid,name,contact,tel,latitude,longitude,province_id,city_id,area_id,specific_address,province_name,city_name,area_name,position,position_name,img_url,from_type,remark,encry);
    }

    //获取所以店铺信息
    public Observable<StoreInfo> getStoreList(String rf, String ts, String v, String i, String udid, String eid, String page_size,String page_index,String encry){
        return mRetrofitService.getStoreList(rf,ts,v,i,udid,eid,page_size,page_index,encry);
    }

    //首页数据统计
    public Observable<StoreList> getStatistics(String rf, String ts, String v, String i, String udid, String eid, String encry){
        return mRetrofitService.getStatistics(rf,ts,v,i,udid,eid,encry);
    }

    //首页数据统计
    public Observable<BaseResponseEntity<String>> getMessage(String rf, String ts, String v, String i, String udid, String message_id, String encry){
        return mRetrofitService.getMessage(rf,ts,v,i,udid,message_id,encry);
    }

    //地图展现、统计店铺 位置可以使用
    public Observable<BaseResponseEntity<List<Simple>>> getSimple(String rf, String ts, String v, String i, String udid, String eid, String page_size, String page_index, String encry){
        return mRetrofitService.getSimple(rf,ts,v,i,udid,eid,page_size,page_index,encry)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }
    public Observable<BaseResponseEntity<Simple>> getSimple(RequestBody requestBody){
        return mRetrofitService.getSimple(requestBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }
}
